package fms.models;
 
import java.util.List;
 
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import fms.classes.TreasurersCashBook;
 
 
public class TreasurersCashBookDAO {
 
    private SqlSessionFactory sqlSessionFactory;
 
    public TreasurersCashBookDAO(){
    	try {
    		sqlSessionFactory = MyBatisSqlConnection.getSqlSessionFactory();
    	} catch(Exception e){
    		System.out.println("failed to get sqlSessionFactory");
    	}
    }
 
    /**Returns the list of all TreasurersCashBook instances from the database*/
    @SuppressWarnings("unchecked")
    public List<TreasurersCashBook> selectAll(){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            List<TreasurersCashBook> list = session.selectList("TreasurersCashBook.getAll");
            return list;
        } finally {
            session.close();
        }
    }
 
    /**Returns a TreasurersCashBook instance from the database*/
    public TreasurersCashBook selectById(int entry_no){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            TreasurersCashBook TreasurersCB = (TreasurersCashBook) session.selectOne("TreasurersCashBook.getById", entry_no);
            return TreasurersCB;
        } finally {
            session.close();
        }
    }
 
    /**Updates an instance of TreasurersCashBook in the database*/
    public void update(TreasurersCashBook TreasurersCB){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            session.update("TreasurersCashBook.update", TreasurersCB);
            session.commit();
        } finally {
            session.close();
        }
    }
 
    /** Insert an instance of TreasurersCashBook into the database*/
    public void insert(TreasurersCashBook TreasurersCB){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            session.insert("TreasurersCashBook.insert", TreasurersCB);
            session.commit();
        } finally {
            session.close();
        }
    }
 
    /** Delete an instance of TreasurersCashBook from the database*/
    public void delete(int entry_no){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            session.delete("TreasurersCashBook.deleteById", entry_no);
            session.commit();
        } finally {
            session.close();
        }
    }
}